﻿Imports TBCC.UtilityMobile
Public Class AIZeroReadForm

    Private _pi As ProjectItem

    '选择设备的网络地址
    Private _netID As Byte = g_netID

    'AI数据的起始地址
    Private _offset As UShort = 20000

    '数据长度
    Private _length As UShort = 4

    Private _UnActived As Single = -500

    Private Sub AIValueForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _pi = Me.MyParentForm.PI

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Car '移动车载
                _length = 4
            Case ProjectType_Enum.Box '小批零
                _length = 1
        End Select

        SetPanelLayout(_pi.ProjectType)
        SetToolTip(_netID)
        'ClearData()

        'Me.HideAllControl()

    End Sub


    Private Sub ClearData()
        For Each ctl As Control In Me.ReadPanel.Controls
            If ctl.GetType Is GetType(TextBox) Then
                CType(ctl, TextBox).Text = String.Empty
            End If
        Next
    End Sub

    Private Sub SetPanelLayout(ByVal DevType As Integer)

        txtAI1.Tag = 0
        txtAI2.Tag = 1
        txtAI3.Tag = 2
        txtAI4.Tag = 3
        'txtAI5.Tag = 4
        'txtAI6.Tag = 5

        '  txtAI1.Enabled = True
        If DevType = 1 Then '移动车载终端
            txtAI1.Enabled = True
            txtAI2.Enabled = True
            txtAI3.Enabled = True
            txtAI4.Enabled = True
        ElseIf DevType = 2 Then '小批零终端

        End If

        Label1.Enabled = txtAI1.Enabled
        Label2.Enabled = txtAI2.Enabled
        Label3.Enabled = txtAI3.Enabled
        Label4.Enabled = txtAI4.Enabled
        'Label5.Visible = txtAI5.Visible
        'Label6.Visible = txtAI6.Visible
    End Sub


    ''' <summary>
    ''' 刷新AI数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RefreshData()
        Dim buf() As UShort
        buf = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordReadonly, _offset, _length)

        If buf Is Nothing Then
            'ClearData()
            'msg.ShowWarning("读数据失败")
            Exit Sub
        End If

        Dim txt As TextBox
        For i As Integer = 0 To _length - 1
            txt = txtList(i)
            If buf(txt.Tag) <> 0 Then
                txt.Text = buf(txt.Tag)
            End If
        Next

    End Sub

    ''' <summary>
    ''' 关闭窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Timer1.Stop()
        ' Me.Close()
    End Sub

    ''' <summary>
    ''' 开始读
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        Me.btnRead.Enabled = False
        Me.btnStop.Enabled = True
        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(False)
        End If
        Me.Timer1.Start()
    End Sub

    ''' <summary>
    ''' 停止读
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.btnRead.Enabled = True
        Me.btnStop.Enabled = False
        Me.Timer1.Stop()
        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(True)
        End If
    End Sub

    Private _isReading As Boolean = False

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If _isReading Then
            Exit Sub
        End If
        Application.DoEvents()

        _isReading = True

        Me.Cursor = Cursors.WaitCursor
        RefreshData()
        Me.Cursor = Cursors.Default

        _isReading = False
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.ClearData()
    End Sub

    ''' <summary>
    ''' 关闭窗体
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        Me.Timer1.Stop()
        MyBase.OnClosing(e)
    End Sub

    Private labList(3) As Label
    Private txtList(3) As TextBox
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        labList(0) = Label1
        labList(1) = Label2
        labList(2) = Label3
        labList(3) = Label4
        'labList(4) = Label5
        'labList(5) = Label6
        For Each l As Label In labList
            ToolTip1.SetToolTip(l, "未使用")
        Next

        txtList(0) = txtAI1
        txtList(1) = txtAI2
        txtList(2) = txtAI3
        txtList(3) = txtAI4
        'txtList(4) = txtAI5
        'txtList(5) = txtAI6
        For Each n As TextBox In txtList
            ToolTip1.SetToolTip(n, "未使用")
        Next
    End Sub

    Private Sub SetToolTip(ByVal NetID As Integer)
        ' Dim portNo As Integer
        ' Dim str As String
        Dim portType As String = "未知"
        Dim rts As New RefTypeService(_pi)

        For i As Integer = 0 To _length - 1
            ToolTip1.SetToolTip(labList(i), "未使用")
            ToolTip1.SetToolTip(txtList(i), "未使用")
        Next

        'For Each rti As RefTypeItem In rts.GeRefGroupList()
        '    If rti.NetId = NetID Then
        '        Dim ats As New AiPortTypeService(rti)
        '        For Each ati As AiPortTypeItem In ats.GetAiGroupList()
        '            portNo = ati.PortNo - 1
        '            If portNo < 0 Then Continue For
        '            If ati.DataType = 1 Then
        '                portType = "温度"
        '            ElseIf ati.DataType = 2 Then
        '                portType = "湿度"
        '            End If
        '            str = String.Format("{0}: {1}({2})", _pi.ProjectName, ati.PortName, portType)
        '            ToolTip1.SetToolTip(labList(portNo), str)
        '            ToolTip1.SetToolTip(txtList(portNo), str)
        '        Next
        '    End If
        'Next

    End Sub

    Private Sub AIZeroReadForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

        Me.Timer1.Stop()

    End Sub
End Class